/*
			       ___ _      _   ___ _  _    ___  _  _ 
			      | __| |    /_\ / __| || |  / _ \| \| |
			      | _|| |__ / _ \\__ \ __ | | (_) | .` |
			      |_| |____/_/ \_\___/_||_|  \___/|_|\_|
			                                            
			  _____ _  _   _ _____   ___ ___ _____ ___ _  _ 
			 |_   _| || | /_\_   _| | _ )_ _|_   _/ __| || |
			   | | | __ |/ _ \| |   | _ \| |  | || (__| __ |
			   |_| |_||_/_/ \_\_|   |___/___| |_| \___|_||_|
			        F L A S H  O N  T H A T  B I T C H
			        
		+--------------------------------------------------------+
		|	This file is delivered as is. Any modifications to   |
		|   this class and/or sub classes may cause errors and   |
		|   can cause you compilations to fail. FOTB takes no	 |
		|   responsibility for any complications caused by the	 |
		|   following code or any modified versions of it.		 |
		+--------------------------------------------------------+

*/
package com.flashonthatbitch.icons {

/*
		 ___ __  __ ___  ___  ___ _____ 
		|_ _|  \/  | _ \/ _ \| _ \_   _|
		 | || |\/| |  _/ (_) |   / | |  
		|___|_|  |_|_|  \___/|_|_\ |_|  
		I M P O R T
*/
	
	import flash.display.Loader;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	public class SilkIcons {
		
		private static var $packagePath			:File;
		private static var $icons				:Array;
		private static var $fileStream			:FileStream;

/*		
		 ___ _   _ ___ _    ___ ___ 
		| _ \ | | | _ ) |  |_ _/ __|
		|  _/ |_| | _ \ |__ | | (__ 
		|_|  \___/|___/____|___\___|
		P U B L I C
*/
			
		/**
		 * Registers the url to the SilkPackage.
		 * You may download the Silk icon package at:
		 * http://www.famfamfam.com/lab/icons/
		 *  
		 * @url 			http://www.famfamfam.com/lab/icons/
		 * @param path		Path to the Silk package
		 * @return 			true if registration is successful.
		 * 
		 */
		public static function registerPackage(path:String):Boolean {
			try {
				$packagePath = new File(path);
			}catch(e:*){
				throw getError(e.message);
			}
			if($packagePath.exists && $packagePath.getDirectoryListing().length > 0 && $packagePath.isDirectory){
				$icons = new Array();
				for each(var icon:File in $packagePath.getDirectoryListing()){
					$icons.push(icon.name);
				}
				return true;
			}
			return false;
		}
		
		/**
		 * Retrieves a Loader object containing the specified icon.
		 * If the icon is not found a SilkIconsError will be thrown.
		 * The icon is loaded directly (not async).
		 * 
		 * @throws			SilkIconsError 
		 * @param name		Icon name
		 * @return 			Loader object with icon
		 * 
		 */
		public static function getIconByName(name:String):Loader {
			if($packagePath){
				try {
					var file:File = new File($packagePath.nativePath + File.separator + name + ".png");
					if(file.exists){
						if(!$fileStream)	$fileStream = new FileStream();
						var l:Loader = new Loader();
						var ba:ByteArray = new ByteArray();
						$fileStream.open(file,FileMode.READ);
						$fileStream.readBytes(ba);
						$fileStream.close();
						l.loadBytes(ba);
						return l;
					}else{
						throw getError("No such icon ("+name+") found in folder " + $packagePath);
					}
				}catch(e:*){
					throw getError(e.message);
				}
			}else{
				throw getError("SilkIcons is not registered. Please use SilkIcons.registerPackage!");
			}
			return null;
		}

/*
		  ___ ___  _  _ ___ _____ ___ _   _  ___ _____ ___  ___ 
		 / __/ _ \| \| / __|_   _| _ \ | | |/ __|_   _/ _ \| _ \
		| (_| (_) | .` \__ \ | | |   / |_| | (__  | || (_) |   /
		 \___\___/|_|\_|___/ |_| |_|_\\___/ \___| |_| \___/|_|_\
		C O N S T R U C T O R
*/
		
		public function SilkIcons() {
			
			throw getError("SilkIcon is a static class and cannot be instanciated. See SilkIcons.registerPackage");
			
		}

/*
		  ___ ___ _____   ___ ___ _____ 
		 / __| __|_   _| / __| __|_   _|
		| (_ | _|  | |   \__ \ _|  | |  
		 \___|___| |_|   |___/___| |_|  
		G E T  S E T
*/		
		/**
		 * [internal-use]
		 * Returns a pre-defined error with the specified message.
		 *  
		 * @param message		Message
		 * @return 				SilkIconsError object
		 * 
		 */
		private static function getError(message:String):Error {
			var err:Error = new Error(message);
			err.name = "SilkIconsError";
			return err;
		}
	}
}